package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxda;
import cn.stylefeng.guns.sys.modular.system.entity.ZxChengben;
import cn.stylefeng.guns.sys.modular.system.entity.ZxChengbenMingxi;
import cn.stylefeng.guns.sys.modular.system.mapper.ZxChengbenMingxiMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxChengbenParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyBxdaResult;
import cn.stylefeng.guns.sys.modular.system.model.result.ZxChengbenResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdaService;
import cn.stylefeng.guns.sys.modular.system.service.ZxChengbenService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * bty_zx_demo控制器
 *
 * @author jiangge
 * @Date 2020-11-02 14:23:32
 */
@Controller
@RequestMapping("/zxChengben")
public class ZxChengbenController extends BaseController {

    private String PREFIX = "/modular/system/zxChengben";

    @Autowired
    private ZxChengbenService zxChengbenService;
    @Resource
    private ZxChengbenMingxiMapper zxChengbenMingxiMapper;

    @Resource
    private BtyBxdaService btyBxdaService;
    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("")
    public String index(Model model, ZxChengbenParam param) {
        //从成本设置跳转过来时，传递门店的id
        model.addAttribute("btyZxMdzxId", param.getBtyZxMdzxId());
        model.addAttribute("btyBrandIdView", param.getBtyBrandIdView());
        return PREFIX + "/zxChengben.html";
    }

    /**
     * 跳转到供货商主页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("index_ghs")
    public String index_ghs(Model model, ZxChengbenParam param) {
        //当前用户
        model.addAttribute("curuser", LoginContextHolder.getContext().getUser());
        //获取报销档案
        String xingming =  LoginContextHolder.getContext().getUser().getName();
        String account =  LoginContextHolder.getContext().getUser().getAccount();
        BtyBxdaResult bxda = btyBxdaService.getByTel("半天妖",account);
        if(bxda ==null){
            BtyBxda mtp = new BtyBxda();
            mtp.setTel(LoginContextHolder.getContext().getUser().getAccount());
            mtp.setXm(xingming);
            mtp.setDm("半天妖");
            btyBxdaService.add(mtp);

            bxda = new BtyBxdaResult();
            bxda.setId(String.valueOf(mtp.getId()));
            bxda.setXm(xingming);
        }

//        if(!bxda.getXm().equals(xingming)){
//            BtyBxda bxdau = btyBxdaService.getById(bxda.getId());
//            bxdau.setXm(xingming);
//            btyBxdaService.updateById(bxdau);
//            bxda.setXm(xingming);
//        }
        model.addAttribute("bxda", bxda);
        //从成本设置跳转过来时，传递门店的id
        model.addAttribute("btyZxMdzxId", param.getBtyZxMdzxId());
        model.addAttribute("btyBrandIdView", param.getBtyBrandIdView());
        return PREFIX + "/zxChengben_ghs.html";
    }

    /**
     * 装修打款
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/zx_dakuan")
    public String dakuan(Model model, ZxChengbenParam param) {
        //从成本设置跳转过来时，传递门店的id
        model.addAttribute("btyZxMdzxId", param.getBtyZxMdzxId());
        model.addAttribute("btyBrandIdView", param.getBtyBrandIdView());
        return PREFIX + "/zxChengben_dakuan.html";
    }

    /**
     * 装修打款
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/zx_choudianbaoxiao")
    public String zx_choudianbaoxiao(Model model, ZxChengbenParam param) {
        //从成本设置跳转过来时，传递门店的id
        model.addAttribute("btyZxMdzxId", param.getBtyZxMdzxId());
        model.addAttribute("btyBrandIdView", param.getBtyBrandIdView());
        return PREFIX + "/zxChengben_choudianbx.html";
    }

    /**
     * 新增页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/zxChengben_add.html";
    }


    /**
     * 新增页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/addGhs")
    public String addGhs(Model model) {
        //当前用户
        model.addAttribute("curuser", LoginContextHolder.getContext().getUser());
        //获取报销档案
        String xingming =  LoginContextHolder.getContext().getUser().getName();
        String account =  LoginContextHolder.getContext().getUser().getAccount();
        BtyBxdaResult bxda = btyBxdaService.getByTel("半天妖",account);
        if(bxda ==null){
            BtyBxda mtp = new BtyBxda();
            mtp.setTel(LoginContextHolder.getContext().getUser().getAccount());
            mtp.setXm(xingming);
            mtp.setDm("半天妖");
            btyBxdaService.add(mtp);

            bxda = new BtyBxdaResult();
            bxda.setId(String.valueOf(mtp.getId()));
            bxda.setTel(account);
            bxda.setXm(xingming);
        }
//        if(!bxda.getXm().equals(xingming)){
//            BtyBxda bxdau = btyBxdaService.getById(bxda.getId());
//            bxdau.setXm(xingming);
//            btyBxdaService.updateById(bxdau);
//            bxda.setXm(xingming);
//        }
        model.addAttribute("bxda", bxda);
        return PREFIX + "/zxChengben_addGhs.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/zxChengben_edit.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/mingxi")
    public String mingxi(Model model) {
        //当前用户
        model.addAttribute("curuser", LoginContextHolder.getContext().getUser());
        //获取报销档案
        String xingming =  LoginContextHolder.getContext().getUser().getName();
        String account =  LoginContextHolder.getContext().getUser().getAccount();

        BtyBxdaResult bxda = btyBxdaService.getByTel("半天妖",account);
        if(bxda ==null){
            BtyBxda mtp = new BtyBxda();
            mtp.setTel(LoginContextHolder.getContext().getUser().getAccount());
            mtp.setXm(xingming);
            mtp.setDm("半天妖");
            btyBxdaService.add(mtp);

            bxda = new BtyBxdaResult();
            bxda.setId(String.valueOf(mtp.getId()));
            bxda.setTel(account);
            bxda.setXm(xingming);
        }
//        if(!bxda.getXm().equals(xingming)){
//            BtyBxda bxdau = btyBxdaService.getById(bxda.getId());
//            bxdau.setXm(xingming);
//            btyBxdaService.updateById(bxdau);
//            bxda.setXm(xingming);
//        }
        model.addAttribute("bxda", bxda);
        return PREFIX + "/zxChengben_mingxi.html";
    }

    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ZxChengbenParam zxChengbenParam) {
        ZxChengbenResult r =  this.zxChengbenService.add(zxChengbenParam);
        return ResponseData.success(r);
    }

    /**
     * 批量新增接口
     * @author
     * @Date
     */
    @RequestMapping("/addMany")
    @ResponseBody
    public ResponseData addMany(ZxChengbenParam zxChengbenParam) {
        System.out.println(zxChengbenParam);
        this.zxChengbenService.addMany(zxChengbenParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ZxChengbenParam zxChengbenParam) {
        this.zxChengbenService.update(zxChengbenParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ZxChengbenParam zxChengbenParam) {
        //查询有明细则不许删除
        QueryWrapper<ZxChengbenMingxi> qw = new QueryWrapper<>();
        qw.eq("bty_zx_chengben_id",zxChengbenParam.getBtyZxChengbenId());
        qw.eq("del_flag",0);
        List<ZxChengbenMingxi> list = zxChengbenMingxiMapper.selectList(qw);
        if(list.size()>0){
            return new ErrorResponseData("该条目下存在明细，请先删除明细。");
        }
        this.zxChengbenService.delete(zxChengbenParam);
        return ResponseData.success();
    }

    /**
     * 打款确认
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/dakuan_queren")
    @ResponseBody
    public ResponseData dakuan_queren(ZxChengbenParam zxChengbenParam) {
        this.zxChengbenService.dakuan_queren(zxChengbenParam);
        return ResponseData.success();
    }

    /**
     * 取消打款确认
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/dakuan_quxiao")
    @ResponseBody
    public ResponseData dakuan_quxiao(ZxChengbenParam zxChengbenParam) {
        this.zxChengbenService.dakuan_quxiao(zxChengbenParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ZxChengbenParam zxChengbenParam) {
        ZxChengben detail = this.zxChengbenService.getById(zxChengbenParam.getBtyZxChengbenId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ZxChengbenParam zxChengbenParam) {
        if(zxChengbenParam.getSearchALL()!=null && zxChengbenParam.getSearchALL().equals("1")){
            zxChengbenParam.setLeixing(null);
        }
        return this.zxChengbenService.findPageBySpec(zxChengbenParam);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/listBh")
    public LayuiPageInfo listBh(ZxChengbenParam zxChengbenParam) {
        if(zxChengbenParam.getSearchALL()!=null && zxChengbenParam.getSearchALL().equals("1")){
            zxChengbenParam.setLeixing(null);
        }
        return this.zxChengbenService.listBh(zxChengbenParam);
    }

    /**
     * 查询列表 打款
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/list_dk")
    public LayuiPageInfo list_dk(ZxChengbenParam zxChengbenParam) {
        zxChengbenParam.setDaKuan("1");
        return this.zxChengbenService.findPageBySpec(zxChengbenParam);
    }


    // 查询列表 打款-导出
    @RequestMapping("/listExport")
    public ResponseEntity<byte[]> exportExcel(HttpServletRequest request, HttpServletResponse response, ZxChengbenParam zxChengbenParam) {
        zxChengbenParam.setDaKuan("1");
        return this.zxChengbenService.exportExcel(request, response,zxChengbenParam);
    }

    /**
     * 成本信息导入
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public ResponseData ddpx(@RequestParam("file") MultipartFile file) throws Exception {

        try {
            String fileName = file.getOriginalFilename();
            ResponseData result = this.zxChengbenService.batchImport(fileName, file);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("解析文件出错");
        }
    }

    /**
     * 成本信息导入-报销单导入
     * 导入excel
     */
    @RequestMapping("/importBaoXiaoDan")
    @ResponseBody
    public ResponseData importBaoXiaoDan(@RequestParam("file") MultipartFile file) throws Exception {

        try {
            String fileName = file.getOriginalFilename();
            ResponseData result = this.zxChengbenService.batchImportBaoXiaoDan(fileName, file);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("解析文件出错");
        }
    }
    /**
     * 投资信息导入
     * 导入excel
     */
    @RequestMapping("/importTz")
    @ResponseBody
    public ResponseData importTz(@RequestParam("file") MultipartFile file) throws Exception {

        try {
            String fileName = file.getOriginalFilename();
            ResponseData result = this.zxChengbenService.importTz(fileName, file);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("解析文件出错");
        }
    }

    /**
     * 投资信息导入
     * 导入excel
     */
    @RequestMapping("/btnImportTzBatch")
    @ResponseBody
    public ResponseData btnImportTzBatch() throws Exception {
        try {
            ResponseData result = this.zxChengbenService.btnImportTzBatch();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("解析文件出错");
        }
    }


    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-12-09
     */
    @RequestMapping("chengbenAndMingXiIndex")
    public String chengbenAndMingXiIndex(Model model, ZxChengbenParam param) {
        //从成本设置跳转过来时，传递门店的id
        model.addAttribute("btyZxMdzxId", param.getBtyZxMdzxId());
        model.addAttribute("btyBrandIdView", param.getBtyBrandIdView());
        return PREFIX + "/zxChengbenAndMingXi.html";
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/chengbenAndMingXiList")
    public LayuiPageInfo chengbenAndMingXiList(ZxChengbenParam zxChengbenParam) {
        if(zxChengbenParam.getSearchALL()!=null && zxChengbenParam.getSearchALL().equals("1")){
            zxChengbenParam.setLeixing(null);
        }
        return this.zxChengbenService.findChengbenAndMingXiPageBySpec(zxChengbenParam);
    }

    /**
     * 装修报销-科目纠错-根据明细中其它科目中的文字与科目对应关系，如果关键字对不上将明细中的科目进行修改
     * @return
     */
    @RequestMapping("/kemuCorrection")
    @ResponseBody
    public ResponseData kemuCorrection() {
        return this.zxChengbenService.kemuCorrection();
    }

    @RequestMapping("/audit")
    @ResponseBody
    public ResponseData audit(ZxChengbenParam param) {
        return this.zxChengbenService.audit(param);
    }

}


